<?php namespace App\Infra;

use App\Models\CommodityType;
use App\Models\Commodity;
use App\Utils\ModelOperatorTrait;

class CommodityManager{

	use ModelOperatorTrait;

	public function addType($data){
		$this->create(CommodityType::class,$data);
	}

	public function getType($id){
		return CommodityType::find($id);
	}

	public function getTypes($conditions = null,$first = false){
		return $this->findModel(CommodityType::class,CommodityType::query(),$conditions,$first)->orderBy('created_at','desc');
	}

	public function updateType($id,$data){
		$this->update(CommodityType::class,$id,$data);
	}

	public function deleteType($id){
		$commodityType = CommodityType::find($id);

		$commodityType->delete();
	}

	public function addCommodity($data){
		$this->create(Commodity::class,$data);
	}

	public function getCommodity($id){
		return Commodity::find($id);
	}

	public function getCommodities($conditions = null,$first = false){
		return $this->findModel(Commodity::class,Commodity::query()->with(['commodityType']),$conditions,$first)->orderBy('created_at','desc');
	}

	public function updateCommodity($id,$data){
		$this->update(Commodity::class,$id,$data);
	}

	public function deleteCommodity($id){
		$commodity = Commodity::find($id);

		$commodity->delete();
	}
}